package linuxbox.common;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.log4j.Logger;

import linuxbox.exception.*;
import linuxbox.BoxStatic;

public abstract class Action {
    protected static Logger logger = Logger.getLogger(Action.class.getName());

    protected ActionServlet servlet = null;

    /**
     * @author kevinz
     *
     * nobody is using this method
     */
    public ActionServlet getServlet() {
        return (this.servlet);
    }

    public void setServlet(ActionServlet servlet) {
        this.servlet = servlet;
    }

    public void doAction(HttpServletRequest aReq, HttpServletResponse aRes)
        throws Exception {
        try {
            HttpSession session = aReq.getSession(false);

            if (session == null)
                throw LinuxboxException.TIMEOUT;

            BoxSession bs =
                (BoxSession) session.getAttribute(BoxStatic.BOX_SESSION);

            if (bs == null) {
                throw LinuxboxException.NO_USER;
            }

            //doAction(null, null, aReq, aRes);
            if (BoxStatic.serviceSwitch == true || bs.isSystemAdmin()) {
                doAction(session, bs, aReq, aRes);
            } else {
                redirectFile(BoxStatic.SERVICE_OFF_PAGE, aReq, aRes);
            }

            //throw new LinuxboxException(ExceptionHelper.SYSTEM, "S000008");
        } catch (Exception exc) {
            sendError(exc, aReq, aRes);
        }
    }

    public void doAction(
                         HttpSession aSession,
                         BoxSession aBoxSession,
                         HttpServletRequest aReq,
                         HttpServletResponse aRes)
        throws Exception {

    }

    public void callAction(
                           String aAction,
                           HttpServletRequest req,
                           HttpServletResponse res) {
        servlet.callAction(aAction, req, res);
    }

    public void sendError(
                          String aErrorID,
                          HttpServletRequest aReq,
                          HttpServletResponse aRes) {
        sendError(
                  new LinuxboxException(ExceptionHelper.SYSTEM, aErrorID),
                  aReq,
                  aRes);
    }

    public void sendError(
                          Exception aException,
                          HttpServletRequest aReq,
                          HttpServletResponse aRes) {
        try {
            logger.error(aException);

            String path =
                aReq.getContextPath() + "/" + BoxStatic.defaultLang + "/jsp/";

            if (aException == LinuxboxException.NO_USER) {
                aRes.sendRedirect(path + BoxStatic.noUserJSP);
            } else if (aException == LinuxboxException.ACCESS_VIOLATION
                       || aException instanceof AccessRightException) {
                aRes.sendRedirect(path + BoxStatic.accessRightErrorJSP);
            } else if (aException == LinuxboxException.TIMEOUT) {
                aRes.sendRedirect(path + BoxStatic.timeoutJSP);
            } else {
                HttpSession httpSession = aReq.getSession();
                if (httpSession != null) {
                    httpSession.setAttribute(BoxStatic.EXCEPTION, aException);
                    aRes.sendRedirect(path + BoxStatic.errorJSP);
                }
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public void sendFile(
                         String aPath,
                         HttpServletRequest aReq,
                         HttpServletResponse aRes) {
        if (logger.isDebugEnabled())
            logger.debug("In Action - sendFile: " + aPath);

        try {
            String path = "/" + BoxStatic.defaultLang + "/jsp/";
            HttpSession session = aReq.getSession(false);

            ServletContext aContext =
                servlet.getServletConfig().getServletContext();
            if (session != null) {
                path += aPath;
            } else {
                path += BoxStatic.timeoutJSP;
            }
            if (logger.isDebugEnabled())
                logger.debug("In Action - sendFile path: " + path);
            if (aContext != null) {
                RequestDispatcher rd = aContext.getRequestDispatcher(path);

                if (rd != null)
                    rd.forward(aReq, aRes);
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public void sendNoneJspFile(
                                String aPath,
                                HttpServletRequest aReq,
                                HttpServletResponse aRes) {
        if (logger.isDebugEnabled())
            logger.debug("In Action - sendNoneJspFile: " + aPath);

        try {
            HttpSession session = aReq.getSession(false);

            ServletContext aContext =
                servlet.getServletConfig().getServletContext();
            if (session == null) {
                aPath = BoxStatic.timeoutJSP;
            }
            if (aContext != null) {
                RequestDispatcher rd =
                    aContext.getRequestDispatcher(
                                                  "/" + BoxStatic.defaultLang + "/" + aPath);

                if (rd != null)
                    rd.forward(aReq, aRes);
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public void redirectFile(
                             String aPath,
                             HttpServletRequest aReq,
                             HttpServletResponse aRes) {
        if (logger.isDebugEnabled())
            logger.debug("In Action - redirectFile: " + aPath);

        try {
            String path =
                aReq.getContextPath() + "/" + BoxStatic.defaultLang + "/jsp/";
            HttpSession session = aReq.getSession(false);

            if (session != null) {
                path += aPath;
            } else {
                path += BoxStatic.timeoutJSP;
            }

            if (logger.isDebugEnabled())
                logger.debug("redirectFile: " + path);
            aRes.sendRedirect(path);
        } catch (Exception e) {
            logger.error(e);
        }
    }
}
